﻿using System;
using System.Collections.Generic;
using AQMProject;

public delegate void OnBeforBuildURLHandler(object sender);
public delegate void OnAfterBuildURLHandler(object sender, Uri url);
public delegate void OnBeforLoadDataHandler(object sender);
public delegate void OnErrorLoadDataHandler(object sender, Exception e);
public delegate void OnAfterLoadDataHandler(object sender, List<ShareData> data);

namespace AQMProject
{

    public class YahooLoader
    {
        public event OnBeforLoadDataHandler OnBeforLoadData;
        public event OnErrorLoadDataHandler OnErrorLoadData;
        public event OnAfterLoadDataHandler OnAfterLoadData;

        public void load(ShareInfo info)
        {
            this.load(info.symbol, info.startDate, info.endDate);
        }

        public void load(string symbol, DateTime startDate, DateTime endDate)
        {
            if (OnBeforLoadData != null)
                OnBeforLoadData(this);

            Uri url = YahooUrlGenerator.createURL(symbol, startDate, endDate);

            try
            {
                if (OnAfterLoadData != null)
                {
                    OnAfterLoadData(this, YahooCsvLoader.readYahhoCsvFromWeb(url));
                }
            }
            catch (Exception e)
            {
                if (OnErrorLoadData != null)
                    OnErrorLoadData(this, e);
            }


        }

    }
}
